home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / sipp / libsipp / planet.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-03  |  1.7 KB  |  90 lines

  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. #include <sipp.h>
  5. #include <geometric.h>
  6. #include <noise.h>
  7.  
  8.  
  9. /* A reasonably nice brown color */
  10. static Color  land = {0.28125, 0.1875, 0.09375};
  11.  
  12. /* Oceans are usually blue */
  13. static Color  sea = {0.0, 0.0, 1.0};
  14.  
  15. /* And Clouds are white */
  16. static Color  cloud = {1.0, 1.0, 1.0};
  17.  
  18.  
  19. /* 
  20.  * This was designed to work with a unit sphere.  
  21.  * 
  22.  * Thanks to Jon Buller       jonb@vector.dallas.tx.us
  23.  */
  24. double
  25. turb(size, scale_factor, loc)
  26.     int size;
  27.     double scale_factor;
  28.     Vector loc;
  29. {
  30.     double cur_scale, result;
  31.     int cur;
  32.  
  33.     result = noise(&loc);
  34.     cur_scale = 1.0;
  35.  
  36.     cur = 1;
  37.     while (cur < size) {
  38.         cur <<= 1;
  39.         cur_scale = cur_scale * scale_factor;
  40.         loc.x *= 2.0;
  41.         loc.y *= 2.0;
  42.         loc.z *= 2.0;
  43.         result += noise(&loc) * cur_scale;
  44.     }
  45.     return result;
  46. }
  47.  
  48.  
  49.  
  50. extern bool noise_ready;
  51.  
  52. void
  53. planet_shader(pos, normal, texture, view_vec, lights, sd, color, opacity)
  54.     Vector      *pos;
  55.     Vector      *normal;
  56.     Vector      *texture;
  57.     Vector      *view_vec;
  58.     Lightsource *lights;
  59.     Surf_desc   *sd;
  60.     Color       *color;
  61.     Color       *opacity;
  62. {
  63.     Vector  tmp;
  64.     double  amt;
  65.  
  66.     if (!noise_ready) {
  67.         noise_init();
  68.     }
  69.  
  70.     VecCopy(tmp, *texture);
  71.  
  72.     if (turb(430, 0.7, tmp) > 0.15)
  73.         sd->color = land;
  74.     else 
  75.         sd->color = sea;
  76.  
  77.     VecScalMul(tmp, 12.0, tmp)
  78.  
  79.     amt = turb(18, 0.6, tmp);
  80.     if (amt > -0.25) {
  81.         amt += 0.25;
  82.         sd->color.red += amt * (cloud.red - sd->color.red);
  83.         sd->color.grn += amt * (cloud.grn - sd->color.grn);
  84.         sd->color.blu += amt * (cloud.blu - sd->color.blu);
  85.     }
  86.  
  87.     basic_shader(pos, normal, texture, view_vec, lights, sd, 
  88.                  color, opacity);
  89. }
  90.